#pragma once

#include <string>
#include "ReadWriteLockHelper.h"

class Data
{
public:
    Data(int size)
    {
        buffer.resize(size);
        lock = new ReadWriteLock();
        for(int i = 0; i < buffer.size(); i++)
        {
            buffer[i] = '*';
        }
    }
    ~Data()
    {
        delete lock;        
    }

public:
    std::string read()
    {
        readLock l(lock);

        return doRead();
    }

    void write(char c)
    {
        writeLock l(lock);

        doWrite(c);
    }

    std::string doRead()
    {
        std::string newbuf;
        newbuf.resize(buffer.size());

        for(int i = 0; i < buffer.size(); i++)
        {
            newbuf[i] = buffer[i];
        }
        slowly();

        return newbuf;
    }

    void doWrite(char c)
    {
        for(int i = 0; i < buffer.size(); i++)
        {
            buffer[i] = c;
            slowly();
        }
    }

    void slowly()
    {
        Sleep(50);
    }

private:
    std::string buffer;
    ReadWriteLock* lock;
};